package com.lishe.lishe.wxapi;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.lishe.lishe.R;
import com.lishe.lishe.lishe.common.Globals;
import com.lishe.lishe.lishe.utils.LoadingUtils;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "WXPayEntryActivity";
    public static final String action_success = "pay_success";
    public static final String action_faild = "pay_faild";
    public static final String action_cancle = "pay_cancle";

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_mall);

        api = WXAPIFactory.createWXAPI(this, null);
        api.registerApp(Globals.WeiXin_appId);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
        Log.e(TAG, "onPayFinish, errCode = " + req.openId);
    }

    @Override
    public void onResp(BaseResp resp) {
        Log.e(TAG, "onPayFinish, resp.errCode = " + resp.errCode + ",resp.errStr = " + resp.errStr+",resp.transaction = "+resp.transaction);
        Intent intent = new Intent();
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            String result = "微信充值失败！";
            if (resp.errCode == 0) {
                result = "微信充值成功！";
                intent.setAction(action_success);
            } else if (resp.errCode == -2) {
                result = "充值已取消！";
                intent.setAction(action_cancle);
            } else if (resp.errCode == -1) {
                result = "微信充值发生未知错误！";
                intent.setAction(action_faild);
            }
            intent.putExtra("result",result);
            Log.e(TAG,"pay result = "+result);
        }
        sendBroadcast(intent);
        finish();
    }
}